package com.lanmao.blog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanmao.blog.entity.Blog;
import com.lanmao.blog.entity.Comment;
import com.lanmao.blog.entity.User;
import com.lanmao.blog.service.BlogService;
import com.lanmao.blog.service.CommentService;
import com.lanmao.blog.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.Authentication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lanmao
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogService blogService;
    @Autowired
    StringRedisTemplate redisTemplate;
    //查询评论列表
    @GetMapping("/comments/{blogId}")
    public R comments(@PathVariable Long blogId) {
        List<Comment> list= commentService.listCommentByBlogId(blogId);
        return R.ok().data("data",list);
    }

    //查询所有评论
    @GetMapping("/getCommentByPage")
    public R getCommentByPage(Long current, Long size){
        Page<Comment> list= commentService.getCommentByPage(current, size);
        return R.ok().data("data",list);
    }

    //根据评论人姓名查询评论
    @GetMapping("/comments")
    public R findCommentByNickname(String nickname){
        List<Comment> list= commentService.findCommentByNickname(nickname);
        return R.ok().data("data",list);
    }


    //新增评论
    @PostMapping("/comments")
    public R post(@RequestBody Comment comment, Authentication authentication, HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        Long blogId = comment.getBlogId();
        if (authentication != null){
            User user = (User)authentication.getPrincipal();
            comment.setAvatar(user.getAvatar());
            comment.setAdminComment(true);
            comment.setIp(ip);
        }
        else {
            //设置头像
            comment.setAvatar("https://edu-lanmao.oss-cn-shenzhen.aliyuncs.com/2022/04/toux.jpg");
            comment.setIp(ip);
        }
        if (comment.getParentComment() != null) {
            comment.setParentCommentId(comment.getParentComment().getId());
        }
        commentService.saveComment(comment);
        Blog blog = blogService.getById(blogId);
        blog.setCommentCount(blog.getCommentCount() + 1);
        blogService.updateByComment(blog);
        List<Comment> list= commentService.listCommentByBlogId(blogId);
        RedisAtomicLong entityIdCounter = new RedisAtomicLong("communitycount", redisTemplate.getConnectionFactory());//评论总数+1
        entityIdCounter.getAndIncrement();
        return R.ok().data("data",list);
    }

    //删除评论
    @DeleteMapping("/deleteComment")
    public String delete(Long id){
        boolean flag=commentService.deleteComment(id);
        RedisAtomicLong entityIdCounter = new RedisAtomicLong("communitycount", redisTemplate.getConnectionFactory());//评论总数-1
        entityIdCounter.getAndDecrement();
        return "OK";
    }

}

